﻿using System;
using System.Threading;
using System.Windows.Input;

namespace SLApp_Test
{
	public class SimpleCommand: ICommand
	{
		public SimpleCommand(Action<object> execute, Predicate<object> canExecute = null)
		{
			ExecuteCommand = execute;
			PredicateCommand = canExecute;
			if (PredicateCommand == null)
			{
				PredicateCommand = (obj) => true;				
			}
		}

		public Action<object> ExecuteCommand { get; set; }
		public Predicate<object> PredicateCommand { get; set; }

		#region Implementation of ICommand
		public bool CanExecute(object parameter)
		{
			return PredicateCommand(parameter);
		}

		public void Execute(object parameter)
		{
			ExecuteCommand(parameter);
		}

		public event EventHandler CanExecuteChanged;

		private void OnCanExecuteChanged(EventArgs e)
		{
			EventHandler handler = Interlocked.CompareExchange(ref CanExecuteChanged, null, null);
			if (handler != null)
			{
				handler(this, e);
			}
		}
		#endregion

		public void RaiseCanExecute()
		{
			OnCanExecuteChanged(EventArgs.Empty);
		}
	}
}